package com.example.modules.common.config.serviceimpl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Random;

/**
 * @program: newtouchaiplatform
 * @description: 腾讯
 * @author: luojm
 * @create: 2018-10-17 11:21
 */
@Service
public class TenCentAuthServiceImpl implements TenCentAuthService {
    Logger logger = (Logger) LoggerFactory.getLogger(TenCentAuthServiceImpl.class);
    private String secretId="AKID66ykOsv3WoxukQ7aw7gnqmHaN1zs4eaE";
    private String secretKey="zriAPi9uTVegov7FG95xahbLyTmAiGh2";
    private long appId = 1256362997;
    private String bucketName="测试";
    private long expired = 2592000;//一个月

    @Override
    public String getAuth() {
        try {
            return appSign(appId,secretId,secretKey,bucketName,expired);
        } catch (Exception e) {
            logger.info("腾讯获取token失败："+e);
        }
        return null;
    }

    @Override
    public Long getAppid() {
        return appId;
    }


    /**
     * 生成 Authorization 签名字段
     *
     * @param appId
     * @param secretId
     * @param secretKey
     * @param bucketName
     * @param expired
     * @return
     * @throws Exception
     */
    public static String appSign(long appId, String secretId, String secretKey, String bucketName,
                                 long expired) throws Exception {
        long now = System.currentTimeMillis() / 1000;
        int rdm = Math.abs(new Random().nextInt());
        String plainText = String.format("a=%d&b=%s&k=%s&t=%d&e=%d&r=%d", appId, bucketName,
                secretId, now, now + expired, rdm);
        byte[] hmacDigest = HmacSha1(plainText, secretKey);
        byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];
        System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);
        System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length,
                plainText.getBytes().length);
        return Base64Encode(signContent);
    }

    /**
     * 生成 base64 编码
     *
     * @param binaryData
     * @return
     */
    public static String Base64Encode(byte[] binaryData) {
        String encodedstr = Base64.getEncoder().encodeToString(binaryData);
        return encodedstr;
    }

    /**
     * 生成 hmacsha1 签名
     *
     * @param binaryData
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] HmacSha1(byte[] binaryData, String key) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        mac.init(secretKey);
        byte[] HmacSha1Digest = mac.doFinal(binaryData);
        return HmacSha1Digest;
    }

    /**
     * 生成 hmacsha1 签名
     *
     * @param plainText
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] HmacSha1(String plainText, String key) throws Exception {
        return HmacSha1(plainText.getBytes(), key);
    }


    public static void main(String[] args) {
        long appId = 1256362997;
        String secretId="AKID66ykOsv3WoxukQ7aw7gnqmHaN1zs4eaE";
        String secretKey="zriAPi9uTVegov7FG95xahbLyTmAiGh2";
        String bucketName="测试";
        long expired = 2592000;//一个月

        String auth = "";
        try {
            auth = appSign(appId,secretId,secretKey,bucketName,expired);
        } catch (Exception e) {

        }
        System.err.println(auth);
    }
}
